﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Liquid.Collections;

namespace GapBufferTests {
	[TestClass]
	public class Deletions {
		public TestContext TestContext { get; set; }

		[TestMethod]
		public void DeleteNextAtStart() {
			var buffer = new GapBuffer<int>(0);
			buffer.InsertRange(new int[] { 1, 2, 3 });
			Assert.AreEqual(3, buffer.Count);
			Assert.IsTrue(buffer.SequenceEqual(new int[] { 1, 2, 3 }), "Got incorrect sequence");
			buffer.Position = 0;
			buffer.DeleteNext();
			Assert.AreEqual(2, buffer.Count);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(new int[] { 2, 3 }), "Got incorrect sequence");
		}

		[TestMethod]
		public void DeletePreviousAtEnd() {
			var buffer = new GapBuffer<int>(0);
			buffer.InsertRange(new int[] { 1, 2, 3 });
			Assert.AreEqual(3, buffer.Count);
			Assert.IsTrue(buffer.SequenceEqual(new int[] { 1, 2, 3 }), "Got incorrect sequence");
			buffer.DeletePrevious();
			Assert.AreEqual(2, buffer.Count);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(new int[] { 1, 2 }), "Got incorrect sequence");
		}

		[TestMethod]
		public void DeleteNextFromMiddle() {
			var buffer = new GapBuffer<int>(0);
			buffer.InsertRange(new int[] { 1, 2, 3 });
			Assert.AreEqual(3, buffer.Count);
			Assert.IsTrue(buffer.SequenceEqual(new int[] { 1, 2, 3 }), "Got incorrect sequence");
			buffer.Position = 1;
			buffer.DeleteNext();
			Assert.AreEqual(2, buffer.Count);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(new int[] { 1, 3 }), "Got incorrect sequence");
		}

		[TestMethod]
		public void DeletePreviousFromMiddle() {
			var buffer = new GapBuffer<int>(0);
			buffer.InsertRange(new int[] { 1, 2, 3 });
			Assert.AreEqual(3, buffer.Count);
			Assert.IsTrue(buffer.SequenceEqual(new int[] { 1, 2, 3 }), "Got incorrect sequence");
			buffer.Position = 2;
			buffer.DeletePrevious();
			Assert.AreEqual(2, buffer.Count);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(new int[] { 1, 3 }), "Got incorrect sequence");
		}


		[TestMethod]
		public void DeleteNextCountAtStart() {
			var buffer = new GapBuffer<int>(0);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.IsTrue(buffer.SequenceEqual(new int[] { 1, 2, 3, 4 }), "Got incorrect sequence");
			buffer.Position = 0;
			buffer.DeleteNext(2);
			Assert.AreEqual(2, buffer.Count);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(new int[] { 3, 4 }), "Got incorrect sequence");
		}

		[TestMethod]
		public void DeletePreviousCountAtEnd() {
			var buffer = new GapBuffer<int>(0);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.IsTrue(buffer.SequenceEqual(new int[] { 1, 2, 3, 4 }), "Got incorrect sequence");
			buffer.DeletePrevious(2);
			Assert.AreEqual(2, buffer.Count);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(new int[] { 1, 2 }), "Got incorrect sequence");
		}

		[TestMethod]
		public void DeleteNextCountFromMiddle() {
			var buffer = new GapBuffer<int>(0);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.IsTrue(buffer.SequenceEqual(new int[] { 1, 2, 3, 4 }), "Got incorrect sequence");
			buffer.Position = 1;
			buffer.DeleteNext(2);
			Assert.AreEqual(2, buffer.Count);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(new int[] { 1, 4 }), "Got incorrect sequence");
		}

		[TestMethod]
		public void DeletePreviousCountFromMiddle() {
			var buffer = new GapBuffer<int>(0);
			buffer.InsertRange(new int[] { 1, 2, 3, 4 });
			Assert.AreEqual(4, buffer.Count);
			Assert.IsTrue(buffer.SequenceEqual(new int[] { 1, 2, 3, 4 }), "Got incorrect sequence");
			buffer.Position = 3;
			buffer.DeletePrevious(2);
			Assert.AreEqual(2, buffer.Count);
			Assert.IsTrue(buffer.ToArray().SequenceEqual(new int[] { 1, 4 }), "Got incorrect sequence");
		}
	}
}
